package com.ruiyian.bridge.info.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.ruiyian.bridge.info.domain.DipmonitorInfo;
import com.ruiyian.bridge.info.mapper.DipmonitorInfoMapper;
import com.ruiyian.bridge.info.service.DipmonitorInfoService;
import com.ruiyian.bridge.project.domain.vo.DataAnalysisDto;
import com.ruiyian.bridge.project.domain.vo.DataAnalysisVo;
import com.ruiyian.bridge.project.domain.vo.Mmh2oQueryBo;
import com.ruiyian.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chong
 * @email 673426164@qq.com
 * @create 2022/6/17
 * Desc: 倾角
 */
@Service
public class DipmonitorInfoServiceImpl extends ServiceImpl<DipmonitorInfoMapper, DipmonitorInfo> implements DipmonitorInfoService {
    @Autowired
    private DipmonitorInfoMapper dipmonitorInfoMapper;

    @Override
    public DataAnalysisVo getNowMinData(String addr, String macaddr) {
        String now = DateUtils.dateTimeNow();
        String nextDate = DateUtils.getNextDate(2, new Date(), -8, null);
        return dipmonitorInfoMapper.getNowMinData(addr, macaddr,nextDate,now);
    }

    /**
     * 同步远程数据
     * @param startNum
     * @param endNum
     * @return
     */
    @DS("bsalve")
    @Override
    public List<DataAnalysisVo> getDataList(Long startNum, Long endNum) {
        return dipmonitorInfoMapper.getDataList(startNum, endNum);
    }

    @Override
    public List<DataAnalysisVo> getNowMinDataByType(DataAnalysisDto dataAnalysisDto) {
        return dipmonitorInfoMapper.getNowMinDataByType(dataAnalysisDto);
    }

    @Override
    public DataAnalysisVo queryNowDisByDateType(DataAnalysisDto dataAnalysisDto) {
        return null;
    }

    /**
     * 本地个数
     * @return
     */
    @Override
    public Long queryCount() {
        return dipmonitorInfoMapper.queryCount();
    }
    /**
     * 远程个数
     * @return
     */
    @DS("bsalve")
    @Override
    public Long queryRemoteCount() {
        return dipmonitorInfoMapper.queryRemoteCount();
    }

    /**
     * 统计分析
     * @param bo
     * @return
     */
    @Override
    public Map<String, Object> queryDataListByAddr(Mmh2oQueryBo bo) {
        Map<String,Object> retrurnData = Maps.newHashMap();
        List<String> xAxis = Lists.newArrayList();
        List<String> legend = Lists.newArrayList();
        List<Double> xdata = Lists.newArrayList();
        List<Double> ydata = Lists.newArrayList();
        List<Double> zdata = Lists.newArrayList();
        List<Map<String,Object>> series = Lists.newArrayList();
        if(org.apache.commons.lang3.StringUtils.isNotBlank(bo.getSearchTime())){
            bo.setStartTime(bo.getSearchTime()+" 00:00:00");
            bo.setEndTime(bo.getSearchTime()+" 23:59:59");
        }
        List<DataAnalysisVo> list = dipmonitorInfoMapper.queryDataListForSummary(bo);
        list.forEach(detail -> {
            xAxis.add(detail.getCreatTime());
            xdata.add(Double.parseDouble(detail.getXval()));
            ydata.add(Double.parseDouble(detail.getYval()));
            zdata.add(Double.parseDouble(detail.getZval()));

        });
        Map<String,Object> xdataMap = new HashMap<>();
        xdataMap.put("data",xdata);
        xdataMap.put("name","x轴");
        series.add(xdataMap);
        legend.add("x轴");
        Map<String,Object> ydataMap = new HashMap<>();
        ydataMap.put("data",ydata);
        ydataMap.put("name","y轴");
        series.add(ydataMap);
        legend.add("y轴");
        Map<String,Object> zdataMap = new HashMap<>();
        zdataMap.put("data",zdata);
        zdataMap.put("name","z轴");
        series.add(zdataMap);
        legend.add("z轴");
        retrurnData.put("legend",legend);
        retrurnData.put("category",xAxis);
        retrurnData.put("series",series);
        return retrurnData;
    }
    /**
     * 多维度类型统计
     * @param dto
     * @return
     */
    @Override
    public List<DataAnalysisVo> queryByDateType(DataAnalysisDto dto) {
        return dipmonitorInfoMapper.queryByDateType(dto);
    }
}
